package com.jxust.controller;

import com.jxust.pojo.Clazz;
import com.jxust.pojo.ClazzQueryParam;
import com.jxust.pojo.PageResult;
import com.jxust.pojo.Result;
import com.jxust.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {

    @Autowired
    private ClazzService clazzService;
    @GetMapping
    public Result  page(ClazzQueryParam clazzQueryParam) {
        log.info("分页查询",  clazzQueryParam);
        PageResult<Clazz> pageResult = clazzService.page(clazzQueryParam);
        log.info("查询结果：{}", pageResult);
        return Result.success(pageResult);
    }


    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除班级：{}", id);
        try {
            boolean success = clazzService.deleteById(id);
            return success ? Result.success() : Result.error("班级不存在");
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping
    public Result add(@RequestBody Clazz clazz) {
        log.info("新增班级：{}", clazz);
        clazzService.add(clazz);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        log.info("修改班级信息：{}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }

}
